package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.CouponBean.couponlist;
import com.da.lon.wang.xlg.util.XLGDataUtils;

import java.util.ArrayList;

public class CouponAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<couponlist> datas;

    public CouponAdapter(Context context, ArrayList<couponlist> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder3 holder = new Holder3();
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.ouka_coupon_lv_item, null);
            }
            holder.numberView = (TextView) convertView
                    .findViewById(R.id.ouka_coupon_number);
            holder.dateView = (TextView) convertView
                    .findViewById(R.id.ouka_coupon_date);
            holder.priceView = (TextView) convertView
                    .findViewById(R.id.ouka_coupon_price);
            holder.useView = (TextView) convertView
                    .findViewById(R.id.ouka_coupon_use);
            holder.bgLinearLayout = (LinearLayout) convertView
                    .findViewById(R.id.ouka_coupon_bg);

            couponlist couponList = datas.get(position);

            holder.numberView.setText("券号:" + couponList.getCoupon());
            holder.priceView.setText("￥" + couponList.getAmount());
            String date = XLGDataUtils.getDateToString(couponList.getExpire_time(),
                    "yyyy年MM月dd日");
            holder.dateView.setText("有效期至:" + date);
            if (null != couponList.getType() && Integer.parseInt(couponList.getType()) == 1) {
                holder.bgLinearLayout
                        .setBackgroundResource(R.drawable.ouka_coupon_bg2);
                holder.priceView.setText("免邮券");
            } else {
                holder.bgLinearLayout
                        .setBackgroundResource(R.drawable.ouka_coupon_bg1);
            }

            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }
}

class Holder3 {

    TextView numberView, dateView, priceView, useView;
    LinearLayout bgLinearLayout;

}
